package data;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.StringTokenizer;

import lucene.PaperSearcher;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.ResultSet;
import com.mysql.jdbc.Statement;

public class UserTagPaper {
	private Connection conn;
	private HashMap<String, Long> authorMap = new HashMap<String, Long>();
	private Statement stmt = null;
	private ResultSet rs = null;
	private PaperSearcher searcher;

	public UserTagPaper() throws Exception {
		Class.forName("com.mysql.jdbc.Driver").newInstance();
		String url = "jdbc:mysql://localhost:3306/" + "bibsonomy";
		String dbuser = "root";
		String dbpass = "123456";
		Connection con = (Connection) DriverManager.getConnection(url, dbuser,
				dbpass);
		this.conn = con;
		File indexDir = new File("D:\\CityU\\project\\bibsonomy\\"
				+ "\\BibIndex\\");
		this.searcher = new PaperSearcher(indexDir);
	}

	public void extractUserPaper() throws Exception {
		Statement stmt = null;
		ResultSet rs = null;
		String path = "D:\\CityU\\project\\bibsonomy\\data" + "\\tags.txt";
		BufferedWriter writer = new BufferedWriter(new FileWriter(path));
		LinkedHashSet<String> set = new LinkedHashSet<String>();
		try {
			stmt = (Statement) conn.createStatement();
			rs = (ResultSet) stmt
					.executeQuery("select user, content_id from tas_new");
			while (rs.next()) {
				String user = rs.getString("user");
				String item = rs.getString("content_id");
				set.add(user + "\t" + item);
			}

			for (String str : set) {
				String[] tokens = str.split("\t");
				String u = tokens[0];
				String i = tokens[1];
				writer.write(u + "," + i + ",1.0");
				writer.newLine();
			}

		} finally {
			writer.flush();
			writer.close();
			if (rs != null) {
				try {
					rs.close();
				} catch (SQLException sqlEx) {
				} // ignore
				rs = null;
			}

			if (stmt != null) {
				try {
					stmt.close();
				} catch (SQLException sqlEx) {
				} // ignore
				stmt = null;
			}
			this.conn.close();
		}

	}

	private void initAuthorMap() throws Exception {
		String path = "D:\\CityU\\project\\bibsonomy\\data" + "\\authorMap.txt";
		BufferedReader reader = new BufferedReader(new FileReader(path));
		String line = null;
		while ((line = reader.readLine()) != null) {
			line = line.trim();
			//System.out.println(line);
			String[] tokens = line.split("\t");
			String author = tokens[0];
			String id = tokens[1];
			authorMap.put(author, Long.valueOf(id));
		}

	}

	private HashSet<Long> extractAuthor(String item) throws Exception {
		HashSet<Long> authors = new HashSet<Long>();
		String au = searcher.retrieveByPid(item).get("author");

		StringTokenizer tokens = new StringTokenizer(au);
		while (tokens.hasMoreTokens()) {
			String str = tokens.nextToken();
			Long id = Long.valueOf(str);
			//Long id = 1L;			
			authors.add(id);
		}

		return authors;
	}

	public void extractUserAuthor() throws Exception {
		initAuthorMap();

		String path = "D:\\CityU\\project\\bibsonomy\\data"
				+ "\\user_author.txt";
		BufferedWriter writer = new BufferedWriter(new FileWriter(path));
		//LinkedHashMap<String, Integer> map = new LinkedHashMap<String, Integer>();
		HashMap<String, Integer> map = new HashMap<String, Integer>();
		try {
			stmt = (Statement) conn.createStatement();
			String path2 = "D:\\CityU\\project\\bibsonomy\\data" + "\\tags.txt";
			BufferedReader reader = new BufferedReader(new FileReader(path2));
			String line = null;
			int lineNum = 1;
			while ((line = reader.readLine()) != null) {
				line = line.trim();
				System.out.println(lineNum);
				//System.out.println(line);
				String[] ts = line.split(",");
				String user = ts[0];
				String item = ts[1];
				HashSet<Long> authors = extractAuthor(item);
				for (long author : authors) {
					String key = user + "\t" + author;
					Integer cnt = map.get(key);
					if (cnt == null) {
						cnt = 0;
					}
					cnt++;
					map.put(key, cnt);
				}
				authors = null;
				lineNum++;
			}

			for (Map.Entry<String, Integer> entry : map.entrySet()) {
				String key = entry.getKey();
				int v = entry.getValue();
				String[] tokens = key.split("\t");
				String u = tokens[0];
				String author = tokens[1];
				writer.write(u + "," + author + "," + v);
				writer.newLine();
			}

		} finally {
			writer.flush();
			writer.close();
			if (rs != null) {
				try {
					rs.close();
				} catch (SQLException sqlEx) {
				} // ignore
				rs = null;
			}

			if (stmt != null) {
				try {
					stmt.close();
				} catch (SQLException sqlEx) {
				}
				stmt = null;
			}
			this.conn.close();
		}

	}

	public static void main(String[] args) throws Exception {
		UserTagPaper app = new UserTagPaper();
		//app.extractUserPaper();
		app.extractUserAuthor();

	}
}
